﻿import com.dmotiko.general.*;
import com.dmotiko.util.*;
import com.dmotiko.prune.*;
import mx.transitions.*;
import mx.transitions.easing.*;
import mx.utils.*;
/**
 * ...
 * @author sminutoli
 */
class FashionGallery extends BaseClip
{
	
	public var btnPage1:MainBtn;
	public var btnPage2:MainBtn;
	private var btn_listener:Function;
	private var miniContainer:MovieClip;
	private var photo_listener:Function;
	private var activeMini:MovieClip;
	
	private function initClip():Void {
		btn_listener = Delegate.create( this, btn_changed );
		photo_listener = Delegate.create( this, show_photo );
		var aPage1:Array = new Array();
		var aPage2:Array = new Array();
		var sScreen:String = Stage.width > 1024 ? "" : "_1024";
		for (var i:Number = 1; i <= 14; i++) {
			//cargo el index para despues usarlo en el zoom
			aPage1.push( { thumb: "fashion/" + i + sScreen + "_mini.jpg", big: "fashion/"+i+sScreen+".jpg", index: i - 1 } );
			aPage2.push( { thumb: "fashion/" + (i+14) + sScreen + "_mini.jpg", big: "fashion/"+ (i+14) + sScreen +".jpg", index: i - 1 } );
		}
		setData( { page1: aPage1, page2: aPage2 } );
	}
	
	private function show_photo(e):Void 
	{
		trace("FashionGallery | show_photo = " + e.data.big);
		//hideMinis();
		activeMini = e.target;
		Site.getApp().getClips().getFashionZoom().setData( e.data );
	}
	
	private function btn_changed(e):Void 
	{
		//trace("FashionGallery | btn_changed= " + e.target.data.value);
		if ( miniContainer ) {
			hideMinis( e.target.data.value );
		} else {
			createMinis(e.target.data.value);
		}
		
	}
	
	private function createMinis(data):Void
	{
		//trace("FashionGallery | createMinis = " + data.length);
		var aMinis:Array = data;
		miniContainer = createEmptyMovieClip("miniContainer", miniContainer ? miniContainer.getDepth() : getNextHighestDepth() );
		miniContainer.clips = new Array();
		
		var nX:Number = 0;
		var nY:Number = 0;
		for (var i:Number = 0; i < aMinis.length; i++) 
		{
			var clip:BaseClip = BaseClip( miniContainer.attachMovie( "FashionGalleryItem", "mini" + i + getTimer(), i ) );
			if ( i > 0 && i % 7 == 0 ) {
				nX = 0;
				nY += clip._height;
			}
			clip._x = nX;
			clip._y = nY;
			aMinis[i].total = aMinis;
			clip.setData( aMinis[i] );
			clip.addEventListener( BaseClipEvent.CHANGED, photo_listener );
			nX += clip._width;
			
			miniContainer.clips.push( clip );
		}
		showMinis();
	}
	
	private function showMinis():Void {
		var check:MovieClip = miniContainer.createEmptyMovieClip( "check", miniContainer.getNextHighestDepth() );
		check.nMini = 0;
		var randomClips:Array = ArrayUtil.randomArray( miniContainer.clips );
		check.onEnterFrame = function() {
			if ( !randomClips[ this.nMini ] ) {
				delete this.onEnterFrame;
				this.removeMovieClip();
				return;
			}
			randomClips[ this.nMini ].show();
			this.nMini++;
		}
	}
	private function hideMinis(data):Void {
		var check:MovieClip = miniContainer.createEmptyMovieClip( "check", miniContainer.getNextHighestDepth() );
		check.nMini = 0;
		var randomClips:Array = ArrayUtil.randomArray( miniContainer.clips );
		var root:FashionGallery = this;
		check.onEnterFrame = function() {
			if ( !randomClips[ this.nMini ] ) {
				delete this.onEnterFrame;
				this.removeMovieClip();
				if(data) root.createMinis( data );
				return;
			}
			randomClips[ this.nMini ].hide();
			this.nMini++;
		}
	}
	
	public function childLoaded( e ):Void {
		switch( e.target ) {
			case btnPage1:
			e.target.setData( { label: "1", value: data.page1 } );
			e.target.addEventListener( BaseClipEvent.CHANGED, btn_listener );
			e.target.onRollOver()
			e.target.setActive(true);
			break;
			
			case btnPage2:
			e.target.setData( { label: "2", value: data.page2 } );
			e.target.addEventListener( BaseClipEvent.CHANGED, btn_listener );
			break;
		}
		
	}
		
}